<!DOCTYPE HTML>
<html lang="en" >
    
    <head>
        
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>全文检索 | 欢迎学习django课程</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-search/search.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
    

        
    
    
    <link rel="next" href="../part6/5.html" />
    
    
    <link rel="prev" href="../part6/3.html" />
    

        
    </head>
    <body>
        
        
    <div class="book"
        data-level="6.3"
        data-chapter-title="全文检索"
        data-filepath="part6/4.md"
        data-basepath=".."
        data-revision="Tue Jan 03 2017 08:43:08 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        课程介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="part1/1.html">
            
                
                    <a href="../part1/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        入门
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.1" data-path="part1/2.html">
            
                
                    <a href="../part1/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.1.</b>
                        
                        搭建开发环境
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="part1/3.html">
            
                
                    <a href="../part1/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.2.</b>
                        
                        设计模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="part1/4.html">
            
                
                    <a href="../part1/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.3.</b>
                        
                        管理站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="part1/5.html">
            
                
                    <a href="../part1/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.4.</b>
                        
                        视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="part1/6.html">
            
                
                    <a href="../part1/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.5.</b>
                        
                        模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="part1/7.html">
            
                
                    <a href="../part1/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="2" data-path="part2/1.html">
            
                
                    <a href="../part2/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        模型
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="part2/2.html">
            
                
                    <a href="../part2/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        定义模型
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="part2/3.html">
            
                
                    <a href="../part2/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        模型成员
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="part2/4.html">
            
                
                    <a href="../part2/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        模型查询
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="part2/5.html">
            
                
                    <a href="../part2/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        自连接
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.5" data-path="part2/6.html">
            
                
                    <a href="../part2/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="part3/1.html">
            
                
                    <a href="../part3/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        视图
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="part3/2.html">
            
                
                    <a href="../part3/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        URLconf
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="part3/3.html">
            
                
                    <a href="../part3/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        视图函数
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="part3/4.html">
            
                
                    <a href="../part3/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        Request对象
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.3.1" data-path="part3/4_1.html">
            
                
                    <a href="../part3/4_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.1.</b>
                        
                        QueryDict对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.2" data-path="part3/4_2.html">
            
                
                    <a href="../part3/4_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.2.</b>
                        
                        GET属性
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3.3" data-path="part3/4_3.html">
            
                
                    <a href="../part3/4_3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.3.</b>
                        
                        POST属性
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3.4" data-path="part3/5.html">
            
                
                    <a href="../part3/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.4.</b>
                        
                        Response对象
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.5" data-path="part3/6.html">
            
                
                    <a href="../part3/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.5.</b>
                        
                        状态保持
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.6" data-path="part3/7.html">
            
                
                    <a href="../part3/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="part4/1.html">
            
                
                    <a href="../part4/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="part4/2.html">
            
                
                    <a href="../part4/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        定义模板
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.2" data-path="part4/3.html">
            
                
                    <a href="../part4/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        模板继承
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="part4/4.html">
            
                
                    <a href="../part4/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        HTML转义
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="part4/5.html">
            
                
                    <a href="../part4/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        CSRF
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.5" data-path="part4/6.html">
            
                
                    <a href="../part4/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.5.</b>
                        
                        验证码
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.6" data-path="part4/7.html">
            
                
                    <a href="../part4/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="part5/1.html">
            
                
                    <a href="../part5/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        高级
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="part5/2.html">
            
                
                    <a href="../part5/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        静态文件处理
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="part5/3.html">
            
                
                    <a href="../part5/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.3" data-path="part5/4.html">
            
                
                    <a href="../part5/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.3.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.4" data-path="part5/5.html">
            
                
                    <a href="../part5/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.4.</b>
                        
                        Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.5" data-path="part5/6.html">
            
                
                    <a href="../part5/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.5.</b>
                        
                        分页
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.6" data-path="part5/7.html">
            
                
                    <a href="../part5/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.6.</b>
                        
                        ajax
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.7" data-path="part5/8.html">
            
                
                    <a href="../part5/8.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.7.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="part6/1.html">
            
                
                    <a href="../part6/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        第三方
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="part6/2.html">
            
                
                    <a href="../part6/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        富文本编辑器
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.2" data-path="part6/3.html">
            
                
                    <a href="../part6/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.2.</b>
                        
                        缓存
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="6.3" data-path="part6/4.html">
            
                
                    <a href="../part6/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.3.</b>
                        
                        全文检索
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.4" data-path="part6/5.html">
            
                
                    <a href="../part6/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.4.</b>
                        
                        celery
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.5" data-path="part6/6.html">
            
                
                    <a href="../part6/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.5.</b>
                        
                        布署
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="6.6" data-path="part6/7.html">
            
                
                    <a href="../part6/7.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.6.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="part7/1.html">
            
                
                    <a href="../part7/1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        Git与项目
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="part7/2.html">
            
                
                    <a href="../part7/2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        Git简介
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1.1" data-path="part7/2_2.html">
            
                
                    <a href="../part7/2_2.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.1.</b>
                        
                        远程仓库
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.1.2" data-path="part7/2_1.html">
            
                
                    <a href="../part7/2_1.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.2.</b>
                        
                        本地仓库
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="part7/3.html">
            
                
                    <a href="../part7/3.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        项目设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="part7/4.html">
            
                
                    <a href="../part7/4.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        模型类设计
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="part7/5.html">
            
                
                    <a href="../part7/5.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        使用页面
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="part7/6.html">
            
                
                    <a href="../part7/6.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        总结与作业
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    Published with GitBook
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >欢迎学习django课程</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x5168;&#x6587;&#x68C0;&#x7D22;">&#x5168;&#x6587;&#x68C0;&#x7D22;</h1>
<ul>
<li>&#x5168;&#x6587;&#x68C0;&#x7D22;&#x4E0D;&#x540C;&#x4E8E;&#x7279;&#x5B9A;&#x5B57;&#x6BB5;&#x7684;&#x6A21;&#x7CCA;&#x67E5;&#x8BE2;&#xFF0C;&#x4F7F;&#x7528;&#x5168;&#x6587;&#x68C0;&#x7D22;&#x7684;&#x6548;&#x7387;&#x66F4;&#x9AD8;&#xFF0C;&#x5E76;&#x4E14;&#x80FD;&#x591F;&#x5BF9;&#x4E8E;&#x4E2D;&#x6587;&#x8FDB;&#x884C;&#x5206;&#x8BCD;&#x5904;&#x7406;</li>
<li>haystack&#xFF1A;django&#x7684;&#x4E00;&#x4E2A;&#x5305;&#xFF0C;&#x53EF;&#x4EE5;&#x65B9;&#x4FBF;&#x5730;&#x5BF9;model&#x91CC;&#x9762;&#x7684;&#x5185;&#x5BB9;&#x8FDB;&#x884C;&#x7D22;&#x5F15;&#x3001;&#x641C;&#x7D22;&#xFF0C;&#x8BBE;&#x8BA1;&#x4E3A;&#x652F;&#x6301;whoosh,solr,Xapian,Elasticsearc&#x56DB;&#x79CD;&#x5168;&#x6587;&#x68C0;&#x7D22;&#x5F15;&#x64CE;&#x540E;&#x7AEF;&#xFF0C;&#x5C5E;&#x4E8E;&#x4E00;&#x79CD;&#x5168;&#x6587;&#x68C0;&#x7D22;&#x7684;&#x6846;&#x67B6;</li>
<li>whoosh&#xFF1A;&#x7EAF;Python&#x7F16;&#x5199;&#x7684;&#x5168;&#x6587;&#x641C;&#x7D22;&#x5F15;&#x64CE;&#xFF0C;&#x867D;&#x7136;&#x6027;&#x80FD;&#x6BD4;&#x4E0D;&#x4E0A;sphinx&#x3001;xapian&#x3001;Elasticsearc&#x7B49;&#xFF0C;&#x4F46;&#x662F;&#x65E0;&#x4E8C;&#x8FDB;&#x5236;&#x5305;&#xFF0C;&#x7A0B;&#x5E8F;&#x4E0D;&#x4F1A;&#x83AB;&#x540D;&#x5176;&#x5999;&#x7684;&#x5D29;&#x6E83;&#xFF0C;&#x5BF9;&#x4E8E;&#x5C0F;&#x578B;&#x7684;&#x7AD9;&#x70B9;&#xFF0C;whoosh&#x5DF2;&#x7ECF;&#x8DB3;&#x591F;&#x4F7F;&#x7528;</li>
<li>jieba&#xFF1A;&#x4E00;&#x6B3E;&#x514D;&#x8D39;&#x7684;&#x4E2D;&#x6587;&#x5206;&#x8BCD;&#x5305;&#xFF0C;&#x5982;&#x679C;&#x89C9;&#x5F97;&#x4E0D;&#x597D;&#x7528;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x4E00;&#x4E9B;&#x6536;&#x8D39;&#x4EA7;&#x54C1;</li>
</ul>
<h1 id="&#x64CD;&#x4F5C;">&#x64CD;&#x4F5C;</h1>
<h4 id="1&#x5728;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x4E2D;&#x4F9D;&#x6B21;&#x5B89;&#x88C5;&#x5305;">1.&#x5728;&#x865A;&#x62DF;&#x73AF;&#x5883;&#x4E2D;&#x4F9D;&#x6B21;&#x5B89;&#x88C5;&#x5305;</h4>
<pre><code>pip install django-haystack
pip install whoosh
pip install jieba
</code></pre><h4 id="2&#x4FEE;&#x6539;settingspy&#x6587;&#x4EF6;">2.&#x4FEE;&#x6539;settings.py&#x6587;&#x4EF6;</h4>
<ul>
<li>&#x6DFB;&#x52A0;&#x5E94;&#x7528;</li>
</ul>
<pre><code>INSTALLED_APPS = (
    ...
    &apos;haystack&apos;,
)
</code></pre><ul>
<li>&#x6DFB;&#x52A0;&#x641C;&#x7D22;&#x5F15;&#x64CE;</li>
</ul>
<pre><code>HAYSTACK_CONNECTIONS = {
    &apos;default&apos;: {
        &apos;ENGINE&apos;: &apos;haystack.backends.whoosh_cn_backend.WhooshEngine&apos;,
        &apos;PATH&apos;: os.path.join(BASE_DIR, &apos;whoosh_index&apos;),
    }
}

#&#x81EA;&#x52A8;&#x751F;&#x6210;&#x7D22;&#x5F15;
HAYSTACK_SIGNAL_PROCESSOR = &apos;haystack.signals.RealtimeSignalProcessor&apos;
</code></pre><h4 id="3&#x5728;&#x9879;&#x76EE;&#x7684;urlspy&#x4E2D;&#x6DFB;&#x52A0;url">3.&#x5728;&#x9879;&#x76EE;&#x7684;urls.py&#x4E2D;&#x6DFB;&#x52A0;url</h4>
<pre><code>
urlpatterns = [
    ...
    url(r&apos;^search/&apos;, include(&apos;haystack.urls&apos;)),
]
</code></pre><h4 id="4&#x5728;&#x5E94;&#x7528;&#x76EE;&#x5F55;&#x4E0B;&#x5EFA;&#x7ACB;searchindexespy&#x6587;&#x4EF6;">4.&#x5728;&#x5E94;&#x7528;&#x76EE;&#x5F55;&#x4E0B;&#x5EFA;&#x7ACB;search_indexes.py&#x6587;&#x4EF6;</h4>
<pre><code># coding=utf-8
from haystack import indexes
from models import GoodsInfo


class GoodsInfoIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=True)

    def get_model(self):
        return GoodsInfo

    def index_queryset(self, using=None):
        return self.get_model().objects.all()
</code></pre><h4 id="5&#x5728;&#x76EE;&#x5F55;templatessearchindexes&#x5E94;&#x7528;&#x540D;&#x79F0;&#x4E0B;&#x521B;&#x5EFA;&#x6A21;&#x578B;&#x7C7B;&#x540D;&#x79F0;texttxt&#x6587;&#x4EF6;">5.&#x5728;&#x76EE;&#x5F55;&#x201C;templates/search/indexes/&#x5E94;&#x7528;&#x540D;&#x79F0;/&#x201D;&#x4E0B;&#x521B;&#x5EFA;&#x201C;&#x6A21;&#x578B;&#x7C7B;&#x540D;&#x79F0;_text.txt&#x201D;&#x6587;&#x4EF6;</h4>
<pre><code>#goodsinfo_text.txt&#xFF0C;&#x8FD9;&#x91CC;&#x5217;&#x51FA;&#x4E86;&#x8981;&#x5BF9;&#x54EA;&#x4E9B;&#x5217;&#x7684;&#x5185;&#x5BB9;&#x8FDB;&#x884C;&#x68C0;&#x7D22;
{{ object.gName }}
{{ object.gSubName }}
{{ object.gDes }}
</code></pre><h4 id="6&#x5728;&#x76EE;&#x5F55;templatessearch&#x4E0B;&#x5EFA;&#x7ACB;searchhtml">6.&#x5728;&#x76EE;&#x5F55;&#x201C;templates/search/&#x201D;&#x4E0B;&#x5EFA;&#x7ACB;search.html</h4>
<pre><code>&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
{% if query %}
    &lt;h3&gt;&#x641C;&#x7D22;&#x7ED3;&#x679C;&#x5982;&#x4E0B;&#xFF1A;&lt;/h3&gt;
    {% for result in page.object_list %}
        &lt;a href=&quot;/{{ result.object.id }}/&quot;&gt;{{ result.object.gName }}&lt;/a&gt;&lt;br/&gt;
    {% empty %}
        &lt;p&gt;&#x5565;&#x4E5F;&#x6CA1;&#x627E;&#x5230;&lt;/p&gt;
    {% endfor %}

    {% if page.has_previous or page.has_next %}
        &lt;div&gt;
            {% if page.has_previous %}&lt;a href=&quot;?q={{ query }}&amp;amp;page={{ page.previous_page_number }}&quot;&gt;{% endif %}&amp;laquo; &#x4E0A;&#x4E00;&#x9875;{% if page.has_previous %}&lt;/a&gt;{% endif %}
        |
            {% if page.has_next %}&lt;a href=&quot;?q={{ query }}&amp;amp;page={{ page.next_page_number }}&quot;&gt;{% endif %}&#x4E0B;&#x4E00;&#x9875; &amp;raquo;{% if page.has_next %}&lt;/a&gt;{% endif %}
        &lt;/div&gt;
    {% endif %}
{% endif %}
&lt;/body&gt;
&lt;/html&gt;
</code></pre><h4 id="7&#x5EFA;&#x7ACB;chineseanalyzerpy&#x6587;&#x4EF6;">7.&#x5EFA;&#x7ACB;ChineseAnalyzer.py&#x6587;&#x4EF6;</h4>
<ul>
<li>&#x4FDD;&#x5B58;&#x5728;haystack&#x7684;&#x5B89;&#x88C5;&#x6587;&#x4EF6;&#x5939;&#x4E0B;&#xFF0C;&#x8DEF;&#x5F84;&#x5982;&#x201C;/home/python/.virtualenvs/django_py2/lib/python2.7/site-packages/haystack/backends&#x201D;</li>
</ul>
<pre><code>import jieba
from whoosh.analysis import Tokenizer, Token


class ChineseTokenizer(Tokenizer):
    def __call__(self, value, positions=False, chars=False,
                 keeporiginal=False, removestops=True,
                 start_pos=0, start_char=0, mode=&apos;&apos;, **kwargs):
        t = Token(positions, chars, removestops=removestops, mode=mode,
                  **kwargs)
        seglist = jieba.cut(value, cut_all=True)
        for w in seglist:
            t.original = t.text = w
            t.boost = 1.0
            if positions:
                t.pos = start_pos + value.find(w)
            if chars:
                t.startchar = start_char + value.find(w)
                t.endchar = start_char + value.find(w) + len(w)
            yield t


def ChineseAnalyzer():
    return ChineseTokenizer()
</code></pre><h4 id="8&#x590D;&#x5236;whooshbackendpy&#x6587;&#x4EF6;&#xFF0C;&#x6539;&#x540D;&#x4E3A;whooshcnbackendpy">8.&#x590D;&#x5236;whoosh_backend.py&#x6587;&#x4EF6;&#xFF0C;&#x6539;&#x540D;&#x4E3A;whoosh_cn_backend.py</h4>
<ul>
<li>&#x6CE8;&#x610F;&#xFF1A;&#x590D;&#x5236;&#x51FA;&#x6765;&#x7684;&#x6587;&#x4EF6;&#x540D;&#xFF0C;&#x672B;&#x5C3E;&#x4F1A;&#x6709;&#x4E00;&#x4E2A;&#x7A7A;&#x683C;&#xFF0C;&#x8BB0;&#x5F97;&#x8981;&#x5220;&#x9664;&#x8FD9;&#x4E2A;&#x7A7A;&#x683C;</li>
</ul>
<pre><code>from .ChineseAnalyzer import ChineseAnalyzer 
&#x67E5;&#x627E;
analyzer=StemmingAnalyzer()
&#x6539;&#x4E3A;
analyzer=ChineseAnalyzer()
</code></pre><h4 id="9&#x751F;&#x6210;&#x7D22;&#x5F15;">9.&#x751F;&#x6210;&#x7D22;&#x5F15;</h4>
<ul>
<li>&#x521D;&#x59CB;&#x5316;&#x7D22;&#x5F15;&#x6570;&#x636E;</li>
</ul>
<pre><code>python manage.py rebuild_index
</code></pre><h4 id="10&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x521B;&#x5EFA;&#x641C;&#x7D22;&#x680F;">10.&#x5728;&#x6A21;&#x677F;&#x4E2D;&#x521B;&#x5EFA;&#x641C;&#x7D22;&#x680F;</h4>
<pre><code>&lt;form method=&apos;get&apos; action=&quot;/search/&quot; target=&quot;_blank&quot;&gt;
    &lt;input type=&quot;text&quot; name=&quot;q&quot;&gt;
    &lt;input type=&quot;submit&quot; value=&quot;&#x67E5;&#x8BE2;&quot;&gt;
&lt;/form&gt;
</code></pre>
                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../part6/3.html" class="navigation navigation-prev " aria-label="Previous page: 缓存"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../part6/5.html" class="navigation navigation-next " aria-label="Next page: celery"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-search/lunr.min.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-search/search.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"highlight":{},"search":{"maxIndexSize":1000000},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2}};
    gitbook.start(config);
});
</script>

        
    </body>
    
</html>
